<?php

namespace app\common\model\mall\goods;

use think\Model;
use think\Db;

class Brand extends Model
{
    // 表名
    protected $name = 'mall_goods_brand';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    
    // 追加属性
    protected $append = [

    ];

    public function getBrandimgAttr($value, $data)
    {
        $mallcdn = config("mall.mallcdn");
        if($mallcdn && !stristr($value,$mallcdn))
            return $mallcdn ? $mallcdn.$value : cdnurl($value);
        else
            return cdnurl($value);
    }

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    /**
     * 获取品牌列表（带分页）
     */
    public function pageQuery($pagesize , $id = 0){
        $id = ($id>0)?$id:(int)input('id');
        $where = [];
        $where['b.status']='normal';
        $where['gcb.cat_id']=$id;
        
        $rs = collection($this->alias('b')
        ->join('__MALL_GOODS_CATBRAND__ gcb','gcb.brand_id=b.id','left')
        ->where($where)
        ->field('b.id brand_id,name,brandImg,gcb.cat_id')
        ->group('b.id,gcb.cat_id')
        ->order('b.weigh asc')
        ->paginate($pagesize))->toArray();
        return $rs;
    }

    /**
     * 获取分类品牌列表
     */
    public function listQuery($cat_id){
        $rs = Db::name('mall_goods_catbrand')->alias('l')
        ->join('__MALL_GOODS_BRAND__ b','b.id=l.brand_id and b.status="normal" and l.cat_id='.$cat_id)
        ->field('b.id brand_id,b.name,b.brandImg,l.cat_id')
        ->group('b.id')
        ->order('b.weigh asc')
        ->select();
        return $rs;
    }
    
    /**
     * 商品筛选品牌查询
     */
    public function goodsListQuery($cat_id){
        $rs = Db::name('mall_goods_catbrand')->alias('l')
            ->join('__MALL_GOODS_BRAND__ b','b.id=l.brand_id and b.status="normal" and l.cat_id='.$cat_id)
            ->join('__MALL_GOODS_GOODS__ g','g.brand_id=b.id','inner')
            ->field('b.id brand_id,b.name,b.brandImg')
            ->group('b.id')
            ->order('b.weigh asc')
            ->select();
        return $rs;
    }
    
    
    /**
     * 根据商品id获取可供选择的品牌
     */
    public function canChoseBrands($goods_id){
        $rs = Db::name('mall_goods_catbrand')->alias('l')
            ->join('__MALL_GOODS_BRAND__ b','b.id=l.brand_id and b.status="normal"')
            ->join('__MALL_GOODS_GOODS__ g','g.brand_id=b.id','inner')
            ->where('g.goods_id','in',$goods_id)
            ->field('b.id brand_id,b.name,b.brandImg')
            ->group('b.id')
            ->order('b.weigh asc')
            ->select();
        return $rs;
    }
    /**
     * 根据品牌id获取商品id
     */
    public function getGoodsIds($brandIds){
        $rs = Db::name('mall_goods_goods')->field('id')->where(['status'=>'normal','goodsStatus'=>1])->where('id','in',$brandIds)->select();
        if(!empty($rs)){
            $bIds = [];
            foreach($rs as $k=>$v){
                $bIds[$k] = $v['id'];
            }
            return $bIds;
        }
        return [];
    }
    
}
